/*     */ package dm.jdbc.driver;
/*     */ 
/*     */ import dm.jdbc.dataConvertion.Convertion;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmdbInputStream
/*     */   extends InputStream
/*     */ {
/*  34 */   public DmdbBlob blob = null;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  39 */   private DmdbClob clob = null;
/*     */   
/*     */ 
/*     */ 
/*     */   private int typeFlag;
/*     */   
/*     */ 
/*  46 */   boolean fromRowSet = false;
/*     */   
/*  48 */   String serverEncoding = null;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  53 */   byte[] byteBuffer = null;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  60 */   int currentPos = 0;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static final int blobLen = 32000;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static final int clobLen = 8000;
/*     */   
/*     */ 
/*     */ 
/*  74 */   int curPos = 0;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  80 */   int byteLen = -1;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  85 */   long lob_length = -1L;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public DmdbInputStream(Object o, boolean fromRowSet, String serverEncoding)
/*     */     throws NumberFormatException
/*     */   {
/*  97 */     this.fromRowSet = fromRowSet;
/*  98 */     if (fromRowSet) {
/*  99 */       this.serverEncoding = serverEncoding;
/*     */     }
/*     */     
/* 102 */     if ((o instanceof DmdbBlob)) {
/* 103 */       this.typeFlag = 0;
/* 104 */       this.blob = ((DmdbBlob)o);
/* 105 */     } else if ((o instanceof DmdbClob)) {
/* 106 */       this.typeFlag = 1;
/* 107 */       this.clob = ((DmdbClob)o);
/* 108 */     } else if ((o instanceof byte[])) {
/* 109 */       this.typeFlag = 3;
/* 110 */       this.byteBuffer = ((byte[])o);
/* 111 */       this.curPos = 0;
/* 112 */       if (this.byteBuffer == null) {
/* 113 */         this.byteLen = 0;
/*     */       } else {
/* 115 */         this.byteLen = this.byteBuffer.length;
/*     */       }
/*     */     } else {
/* 118 */       throw new NumberFormatException("cast error.");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected DmdbInputStream(DmdbBlob blob, long pos, long length)
/*     */   {
/* 125 */     this.typeFlag = 0;
/* 126 */     this.blob = blob;
/* 127 */     this.currentPos = ((int)(pos - 1L));
/* 128 */     this.lob_length = length;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int read()
/*     */     throws IOException
/*     */   {
/* 142 */     byte[] buf = new byte[1];
/*     */     
/* 144 */     int retLen = read(buf);
/* 145 */     if (retLen == -1) {
/* 146 */       return -1;
/*     */     }
/*     */     
/* 149 */     return buf[0] & 0xFF;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public int read(byte[] bs)
/*     */     throws IOException
/*     */   {
/* 157 */     switch (this.typeFlag) {
/*     */     case 0: 
/* 159 */       return readFromBlob(bs);
/*     */     
/*     */     case 1: 
/* 162 */       return readFromClob(bs);
/*     */     
/*     */     case 3: 
/* 165 */       return readFromByteArray(bs);
/*     */     }
/*     */     
/* 168 */     throw new IOException(
/* 169 */       "io read error,invalid data type of this stream.");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int available()
/*     */     throws IOException
/*     */   {
/* 191 */     int length = 0;
/*     */     
/* 193 */     switch (this.typeFlag) {
/*     */     case 0: 
/* 195 */       length = this.blob.getLobLen();
/* 196 */       break;
/*     */     case 1: 
/* 198 */       length = this.clob.getLobLen();
/* 199 */       break;
/*     */     case 3: 
/* 201 */       length = this.byteBuffer.length;
/* 202 */       break;
/*     */     case 2: default: 
/* 204 */       throw new IOException("io read error.");
/*     */     }
/* 206 */     return length - this.curPos;
/*     */   }
/*     */   
/*     */   private int readFromBlob(byte[] buf) throws IOException
/*     */   {
/* 211 */     if (buf == null) {
/* 212 */       throw new NullPointerException();
/*     */     }
/* 214 */     int needLen = buf.length;
/* 215 */     int bufPos = 0;
/*     */     
/* 217 */     int readLen = 0;
/*     */     try
/*     */     {
/* 220 */       if (this.blob == null) {
/* 221 */         return -1;
/*     */       }
/*     */       
/* 224 */       if (this.lob_length < 0L) {
/* 225 */         this.lob_length = this.blob.length();
/*     */       }
/* 227 */       if ((this.lob_length <= 0L) || (
/* 228 */         ((this.byteBuffer == null) || (this.curPos >= this.byteLen)) && (this.lob_length <= this.currentPos))) {
/* 229 */         return -1;
/*     */       }
/*     */       
/* 232 */       long invalidLen = 0L;
/* 233 */       if (this.byteBuffer == null) {
/* 234 */         invalidLen = this.lob_length - this.currentPos;
/*     */       } else {
/* 236 */         invalidLen = this.lob_length - this.currentPos + this.byteLen - this.curPos;
/*     */       }
/*     */       
/* 239 */       int destLen = (int)(invalidLen < needLen ? invalidLen : needLen);
/*     */       
/*     */ 
/* 242 */       while (destLen > readLen)
/*     */       {
/* 244 */         if ((this.byteBuffer == null) || (this.curPos >= this.byteBuffer.length)) {
/* 245 */           int bufLen = (int)(32000L > this.lob_length - this.currentPos ? this.lob_length - 
/* 246 */             this.currentPos : 
/* 247 */             32000L);
/*     */           
/* 249 */           this.byteBuffer = this.blob.getBytes(this.currentPos + 1, bufLen);
/*     */           
/* 251 */           if (this.byteBuffer.length <= 0) {
/*     */             break;
/*     */           }
/* 254 */           this.currentPos += bufLen;
/*     */           
/* 256 */           this.curPos = 0;
/* 257 */           this.byteLen = this.byteBuffer.length;
/*     */         }
/*     */         
/*     */ 
/* 261 */         int realLen = this.byteLen - this.curPos > destLen - readLen ? destLen - 
/* 262 */           readLen : this.byteLen - this.curPos;
/*     */         
/* 264 */         System.arraycopy(this.byteBuffer, this.curPos, buf, bufPos, realLen);
/*     */         
/* 266 */         bufPos += realLen;
/* 267 */         this.curPos += realLen;
/*     */         
/* 269 */         readLen += realLen;
/*     */       }
/*     */     }
/*     */     catch (SQLException sqle) {
/* 273 */       throw new IOException(sqle.getMessage());
/*     */     }
/*     */     
/* 276 */     return readLen;
/*     */   }
/*     */   
/*     */   private int readFromClob(byte[] buf) throws IOException
/*     */   {
/* 281 */     if (buf == null) {
/* 282 */       throw new NullPointerException();
/*     */     }
/* 284 */     int needLen = buf.length;
/* 285 */     int bufPos = 0;
/*     */     
/* 287 */     int readLen = 0;
/*     */     try
/*     */     {
/* 290 */       if (this.clob == null) {
/* 291 */         return -1;
/*     */       }
/*     */       
/* 294 */       if (this.lob_length < 0L) {
/* 295 */         this.lob_length = this.clob.length();
/*     */       }
/* 297 */       if ((this.lob_length <= 0L) || (((this.byteBuffer == null) || (this.curPos >= this.byteLen)) && 
/* 298 */         (this.lob_length <= this.currentPos))) {
/* 299 */         return -1;
/*     */       }
/*     */       
/* 302 */       long invalidLen = 0L;
/* 303 */       if (this.byteBuffer == null) {
/* 304 */         invalidLen = this.lob_length - this.currentPos;
/*     */       } else {
/* 306 */         invalidLen = this.lob_length - this.currentPos + this.byteLen - this.curPos;
/*     */       }
/*     */       
/* 309 */       int destLen = (int)(invalidLen < needLen ? invalidLen : needLen);
/*     */       
/*     */ 
/* 312 */       while (destLen > readLen)
/*     */       {
/* 314 */         if ((this.byteBuffer == null) || (this.curPos >= this.byteBuffer.length)) {
/* 315 */           if (this.serverEncoding == null) {
/* 316 */             this.serverEncoding = this.clob.m_conn.getServerEncoding();
/*     */           }
/* 318 */           int bufLen = (int)(8000L > this.lob_length - this.currentPos ? this.lob_length - 
/* 319 */             this.currentPos : 
/* 320 */             8000L);
/* 321 */           String subString = this.clob
/* 322 */             .getSubString(this.currentPos + 1, bufLen);
/* 323 */           this.currentPos += bufLen;
/*     */           
/*     */ 
/* 326 */           if ((subString != null) && (subString.length() > 0))
/*     */           {
/* 328 */             this.byteBuffer = Convertion.getBytes(subString, 0, subString.length(), this.serverEncoding);
/*     */           }
/*     */           else {
/* 331 */             this.byteBuffer = new byte[0];
/* 332 */             break;
/*     */           }
/*     */           
/* 335 */           this.curPos = 0;
/* 336 */           this.byteLen = this.byteBuffer.length;
/*     */         }
/*     */         
/*     */ 
/* 340 */         int realLen = this.byteLen - this.curPos > destLen - readLen ? destLen - 
/* 341 */           readLen : this.byteLen - this.curPos;
/*     */         
/* 343 */         System.arraycopy(this.byteBuffer, this.curPos, buf, bufPos, realLen);
/*     */         
/* 345 */         bufPos += realLen;
/* 346 */         this.curPos += realLen;
/* 347 */         readLen += realLen;
/*     */       }
/*     */     }
/*     */     catch (SQLException sqle) {
/* 351 */       throw new IOException(sqle.getMessage());
/*     */     }
/*     */     
/* 354 */     return readLen;
/*     */   }
/*     */   
/*     */   private int readFromByteArray(byte[] buf)
/*     */   {
/* 359 */     if (buf == null) {
/* 360 */       throw new NullPointerException();
/*     */     }
/* 362 */     int needLen = buf.length;
/*     */     
/* 364 */     if ((this.byteBuffer == null) || ((this.byteLen = this.byteBuffer.length) <= this.curPos)) {
/* 365 */       return -1;
/*     */     }
/*     */     
/* 368 */     int invalidLen = this.byteLen - this.curPos > needLen ? needLen : this.byteLen - 
/* 369 */       this.curPos;
/*     */     
/* 371 */     System.arraycopy(this.byteBuffer, this.curPos, buf, 0, invalidLen);
/* 372 */     this.curPos += invalidLen;
/*     */     
/* 374 */     return invalidLen;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\driver\DmdbInputStream.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */